package de.jmda.gen.impl;

import static de.jmda.core.util.StringUtil.sb;
import static org.junit.Assert.assertTrue;

import org.apache.log4j.Logger;
import org.junit.Test;

import de.jmda.gen.GeneratorException;
import de.jmda.gen.LineIndenter;

public class JUTDefaultGenerator
{
	private final static Logger LOGGER =
      Logger.getLogger(JUTDefaultGenerator.class);

	@Test
	public void testDefaultGeneratorString() throws GeneratorException
	{
		String input = "input";

		DefaultGenerator defaultGenerator = new DefaultGenerator(input);

		StringBuffer generated = defaultGenerator.generate();
		
		LOGGER.debug("generated [" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				input.contentEquals(generated));
	}

	@Test
	public void testDefaultGeneratorStringLineIndenter() throws GeneratorException
	{
		String input = "input";

		LineIndenter lineIndenter = new LineIndenter();

		DefaultGenerator defaultGenerator =
				new DefaultGenerator(input, lineIndenter);

		StringBuffer generated = defaultGenerator.generate();
		
		LOGGER.debug("generated [" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				input.contentEquals(generated));

		String indentation = "indentation";
		int indentationLevel = 1;
		boolean indenting = true;
		
		lineIndenter = new LineIndenter(indentation, indentationLevel, indenting);
		
		defaultGenerator = new DefaultGenerator(input, lineIndenter);

		generated = defaultGenerator.generate();
		
		LOGGER.debug("generated (single indent)[" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				(indentation + input).contentEquals(generated));
		
		indentationLevel = 2;
		
		lineIndenter = new LineIndenter(indentation, indentationLevel, indenting);
		
		defaultGenerator = new DefaultGenerator(input, lineIndenter);

		generated = defaultGenerator.generate();
		
		LOGGER.debug("generated (double indent)[" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				(indentation + indentation + input).contentEquals(generated));
	}

	@Test
	public void testDefaultGeneratorStringBufferLineIndenter()
			throws GeneratorException
	{
		StringBuffer input = sb("input");

		LineIndenter lineIndenter = new LineIndenter();

		DefaultGenerator defaultGenerator =
				new DefaultGenerator(input, lineIndenter);

		StringBuffer generated = defaultGenerator.generate();
		
		LOGGER.debug("generated [" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				input.toString().contentEquals(generated));

		String indentation = "indentation";
		int indentationLevel = 1;
		boolean indenting = true;
		
		lineIndenter = new LineIndenter(indentation, indentationLevel, indenting);
		
		defaultGenerator = new DefaultGenerator(input, lineIndenter);

		generated = defaultGenerator.generate();
		
		LOGGER.debug("generated (single indentation)[" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				(indentation + input).contentEquals(generated));
		
		indentationLevel = 2;
		
		lineIndenter = new LineIndenter(indentation, indentationLevel, indenting);
		
		defaultGenerator = new DefaultGenerator(input, lineIndenter);

		generated = defaultGenerator.generate();
		
		LOGGER.debug("generated (double indent)[" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				(indentation + indentation + input).contentEquals(generated));
	}

	@Test
	public void testSetLineIndenter() throws GeneratorException
	{
		String input = "input";
		
		String indentation = "indentation";
		int indentationLevel = 2;
		boolean indenting = true;

		LineIndenter lineIndenter =
				new LineIndenter(indentation, indentationLevel, indenting);

		DefaultGenerator defaultGenerator = new DefaultGenerator(input);

		defaultGenerator.setLineIndenter(lineIndenter);
		
		StringBuffer generated = defaultGenerator.generate();
		
		LOGGER.debug("generated (double indentation)[" + generated + "]");
		
		assertTrue(
				"unexpected generated [" + generated + "]",
				(indentation + indentation + input).contentEquals(generated));
	}

//	@Test
//	public void testGetOutput() throws GeneratorException
//	{
//		String input = "input";
//		
//		String indentation = "indentation";
//		int indentationLevel = 1;
//		boolean indenting = true;
//
//		LineIndenter lineIndenter =
//				new LineIndenter(indentation, indentationLevel, indenting);
//
//		DefaultGenerator defaultGenerator = new DefaultGenerator(input);
//
//		defaultGenerator.setLineIndenter(lineIndenter);
//		
//		StringBuffer generated = defaultGenerator.generate();
//		StringBuffer output = defaultGenerator.getOutput();
//		
//		LOGGER.debug("generated (double indent)[" + generated + "]");
//		
//		assertTrue(
//				"unexpected generated [" + generated + "]",
//				(indentation + input).contentEquals(generated));
//		assertTrue(
//				"unexpected output [" + output + "]",
//				(input).contentEquals(output));
//	}
}